import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import EventPackage.Event;
import EventPackage.EventManagerInterface;
import EventPackage.EventQueue;


public class Simulator implements ActionListener
{
private JFrame thisFrame;
private JTextArea notificationArea; // textually represents the state of the alarm

private JLabel indicator; // turns Red if any alarm goes off
private JLabel systemState; // indicates
private boolean windowFlag = false;
private String EvtMgrIP;					// Event Manager IP address
private Event Evt = null;					// Event object
private EventQueue eq = null;				// Message Queue
private int EvtId = 0;						// User specified event ID
private EventManagerInterface em = null;	// Interface object to the event manager
private boolean WindowState = false;	// Window state: false == not broken, true == broken
private int	Delay = 2500;					// The loop delay (2.5 seconds)
private boolean Done = false;				// Loop termination flag

private JButton breakButton;
private JButton fixButton;

private JButton fireStartButton;
private JButton fireStopButton;

private JButton motionDetectStartButton;
private JButton motionDetectStopButton;

private JButton doorBreakButton;
private JButton doorFixButton;



public static void main(String args[]){
	Simulator x = new Simulator(args);

}

public Simulator(String args[])
{
initialize();
if ( args.length == 0 )
	{
	// event manager is on the local system

	System.out.println("\n\nAttempting to register on the local machine..." );

	try
	{
		// Here we create an event manager interface object. This assumes
		// that the event manager is on the local machine

		em = new EventManagerInterface();
	}

	catch (Exception e)
	{
		System.out.println("Error instantiating event manager interface: " + e);

	} // catch

} else {

	// event manager is not on the local system

	EvtMgrIP = args[0];

	System.out.println("\n\nAttempting to register on the machine:: " + EvtMgrIP );

	try
	{
		// Here we create an event manager interface object. This assumes
		// that the event manager is NOT on the local machine

		em = new EventManagerInterface( EvtMgrIP, args[1] );
	}

	catch (Exception e)
	{
		System.out.println("Error instantiating event manager interface: " + e);

	} // catch

} // if
}
private void initialize(){
thisFrame = new JFrame();
thisFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisFrame.setLayout(new BorderLayout());
thisFrame.setSize(500,200);

indicator = new JLabel();
indicator.setBackground(Color.GREEN);

systemState = new JLabel("System Off");

notificationArea = new JTextArea();
notificationArea.setEditable(false);

JPanel northPanel = new JPanel(new BorderLayout());
northPanel.add(indicator, BorderLayout.NORTH);
northPanel.add(notificationArea, BorderLayout.CENTER);

thisFrame.add(northPanel, BorderLayout.NORTH);

JPanel centerPanel = new JPanel(new GridLayout(3,4));

breakButton = new JButton("Break Window");
breakButton.addActionListener(this);

fixButton = new JButton("Fix Window");
fixButton.addActionListener(this);

fireStartButton = new JButton("Start Fire");
fireStartButton.addActionListener(this);
fireStopButton = new JButton("Stop Fire");
fireStopButton.addActionListener(this);

doorBreakButton = new JButton("Break Door");
doorBreakButton.addActionListener(this);
doorFixButton = new JButton("Fix Door");
doorFixButton.addActionListener(this);

motionDetectStartButton = new JButton("Initiate Motion");
motionDetectStartButton.addActionListener(this);
motionDetectStopButton = new JButton("Stop Motion");
motionDetectStopButton.addActionListener(this);



centerPanel.add(breakButton);
centerPanel.add(doorBreakButton);
centerPanel.add(motionDetectStartButton);
centerPanel.add(fireStartButton);


centerPanel.add(new JLabel());
centerPanel.add(new JLabel());
centerPanel.add(new JLabel());
centerPanel.add(new JLabel());

centerPanel.add(fixButton);
centerPanel.add(doorFixButton);
centerPanel.add(motionDetectStopButton);
centerPanel.add(fireStopButton);

thisFrame.add(centerPanel, BorderLayout.CENTER);
thisFrame.setVisible(true);
}

public void actionPerformed(ActionEvent arg0) {
JButton button = (JButton)arg0.getSource();

try
{
	eq = em.GetEventQueue();

} // try

catch( Exception e )
{
	System.out.println("Error getting event queue::" + e );

} // catch



if(button == breakButton){


	PostWindowBreakEvent(em, -6, "WB");
	

}
else if (button == fixButton){


	PostWindowBreakEvent(em, -6,  "WF");

}
else if (button == doorBreakButton){


		PostWindowBreakEvent(em, -6,  "DB");

	}
else if (button == doorFixButton){


		PostWindowBreakEvent(em, -6,  "DS");

	}
else if (button == motionDetectStartButton){


		PostWindowBreakEvent(em, -6,  "MD");

	}
else if (button == motionDetectStopButton){


		PostWindowBreakEvent(em, -6,  "MS");

	}
else if (button == fireStartButton){


		PostWindowBreakEvent(em, -8,  "FD");

	}
else if (button == fireStopButton){


		PostWindowBreakEvent(em, -8,  "FS");

	}

}

static private void PostWindowBreakEvent(EventManagerInterface ei, int id, String txt)
{
	// Here we create the event.
	
	Event evt = null; 
	evt = new Event(id, txt);

	// Here we send the event to the event manager.

	try
	{
		ei.SendEvent( evt );
		//mw.WriteMessage( "Sent Humidity Event" );

	} // try

	catch (Exception e)
	{
		System.out.println( "Error Posting Relative Humidity:: " + e );

	} // catch

} // PostHumidity


}
